/**
* ...
* @author Default
* @version 0.1
*/

package  {
	import com.crl.managers.ResizeableManager;
	import com.crl.ui.controls.Component;
	import com.crl.utils.QueryString;
	import com.crlclr.*;
	import com.crlclr.common.*;
	import com.crlclr.events.*;
	import com.crlclr.managers.*;
	import com.crlclr.ui.*;
	import com.crlclr.utils.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.system.*;
	
	
	[SWF(frameRate='30',backgroundColor='#990066')]
	public class PreLoader extends Component {
		private var skinPath:String = "item/skin.swf";
		private var qqcode:String = "13202817";
		private var prefix:String="";
		public function PreLoader() {
			
		}
		
		override protected function defaults() : void{
			stage.align=StageAlign.TOP_LEFT;
			stage.scaleMode=StageScaleMode.NO_SCALE;
			
			Keys.initStage(stage);
			ResizeableManager.getInstance().bindStage(stage);
			SkinManager.getInstance().load(FileInfo.relativelyBy(this)+skinPath);
		}
		
		protected override function create():void {
			addChild(LoaderBar.getInstance());
		}
		
		override protected function bind():void {
			LoaderManager.getInstance().addEventListener(LoaderEvent.START, eventHander);
			LoaderManager.getInstance().addEventListener(ProgressEvent.PROGRESS, eventHander);
			LoaderManager.getInstance().addEventListener(LoaderEvent.END, applicationComplete);
		}
		
		private function unbind():void {
			LoaderManager.getInstance().removeEventListener(LoaderEvent.START, eventHander);
			LoaderManager.getInstance().removeEventListener(ProgressEvent.PROGRESS, eventHander);
			LoaderManager.getInstance().removeEventListener(LoaderEvent.END, eventHander);
		}
		
		private function eventHander(event:Event):void {
			switch(event.type) {
				case LoaderEvent.START :
					LoaderBar.getInstance().show();
					break;
				case ProgressEvent.PROGRESS :
					var ev:ProgressEvent = event as ProgressEvent;
					LoaderBar.getInstance().progress =ev.bytesLoaded/ev.bytesTotal
					break;
				case LoaderEvent.END :
					LoaderBar.getInstance().hide();
					break;
			}
		}
		
		private function applicationComplete(event:Event):void{
			LoaderManager.getInstance().removeEventListener(LoaderEvent.END, applicationComplete);
			LoaderManager.getInstance().addEventListener(LoaderEvent.END, eventHander);
			LoaderBar.getInstance().hide();
			
			this.execute();
		}
		
		
		override protected  function init() : void{
			var loaderinfo:LoaderInfo=this.root.loaderInfo;
			var code:String=loaderinfo.parameters["qq"];
			var prefix:String=loaderinfo.parameters["prefix"];
			
			if(code==null){
				if(QueryString.hasParams){
					code=QueryString.readParams()["code"];
				}
			}
			//prefix="http://www.crllog.com/picasa/dl.asp";
			if(prefix){
				prefix+="?url=";
			}
			
			this.qqcode=(code==null?this.qqcode:code);
			this.prefix=(prefix==null?this.prefix:prefix);
			SiteStatic.init(this.qqcode,this.prefix);
		}
		
		public function execute():void {
			var app:Application = new Application();
			addChild(app);
			app.autoRun();
		}
		
	}
	
}
